﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;

namespace EM.operation.income
{
    public partial class _default : System.Web.UI.Page
    {
        private bool hasFULLAccess = false;
        private bool hasBMAccess = false;
        private bool hasLIMITEDAccess = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(18, Authentication.RedirectionURLTypes.LoginPage);

            hasFULLAccess = ua.HasPageFunctionAuthentication(18, "FULL");
            hasBMAccess = ua.HasPageFunctionAuthentication(18, "BM");
            hasLIMITEDAccess = ua.HasPageFunctionAuthentication(18, "LIMITED");

            if (!IsPostBack)
            {
                FillCombo fc = new FillCombo();
                fc.IncomeType(ddlINCOMETYPE, 0, true, Convert.ToInt32(Session["WORKGROUPID"]));
            }

        }
        private DataTable GetData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";
            sSQL += @"
                    SELECT A.* , C.CURRENCYCODE FROM
                    (SELECT INCOMEID, I.INCOMETYPEID,IT.INCOMETYPENAME,CONVERT(NVARCHAR, I.INCOMEDATE,104) AS INCOMEDATE, CURRENCYID,
                    I.AMOUNT, I.DESCRIPTION   FROM TBLINCOME I, TBLINCOMETYPE IT
                    WHERE I.INCOMETYPEID = IT.INCOMETYPEID AND I.WORKGROUPID = " + Session["WORKGROUPID"];

            if (ddlINCOMETYPE.SelectedValue != "" && ddlINCOMETYPE.SelectedValue != "0")
            {
                sSQL += " AND I.INCOMETYPEID = @INCOMETYPEID";
                cmd.Parameters.AddWithValue("@INCOMETYPEID", ddlINCOMETYPE.SelectedValue);
            }
            if (rdpSTART.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND I.INCOMEDATE >= @EXPENSESTARTDATE";
                cmd.Parameters.AddWithValue("@EXPENSESTARTDATE", rdpSTART.SelectedDate);
            }
            if (rdpSTART.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND I.INCOMEDATE <= @EXPENSEENDDATE";
                cmd.Parameters.AddWithValue("@EXPENSEENDDATE", rdpEND.SelectedDate);
            }
            if (!hasFULLAccess)
            {
                if (hasBMAccess)
                {
                    sSQL += " AND I.BRANCHID = @BRANCHID";
                    cmd.Parameters.AddWithValue("@BRANCHID", Session["BRANCHID"]);

                }
                else
                {
                    sSQL += " AND I.INCOMEUSERID = @INCOMEUSERID";
                    cmd.Parameters.AddWithValue("@INCOMEUSERID", Session["USERID"]);
                }
            }
            sSQL += " ) AS A LEFT OUTER JOIN TBLCURRENCY C ON A.CURRENCYID = C.CURRENCYID ";


            sSQL += "    ORDER BY INCOMEDATE DESC";

            cmd.CommandText = sSQL;

            DataTable dt = new DataTable();
            DbHelper dsrv = new DbHelper();

            dt = dsrv.GetDataTable(cmd, "TBL");
            return dt;
        }

        protected void rgMaster_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = GetData();
            rgMaster.DataSource = dt;

            rgMaster.MasterTableView.NoMasterRecordsText = "Kayıt bulunamadı.";
            rgMaster.StatusBarSettings.ReadyText = dt.Rows.Count + " adet kayıt bulundu.";
        }

        protected void rgMaster_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Pager)
            {
                Label lblRecordCount = new Label();
                e.Item.Controls[0].Controls.AddAt(0, lblRecordCount);
                lblRecordCount.Text = rgMaster.StatusBarSettings.ReadyText + " | sayfaya git : ";

                rgMaster.ShowStatusBar = rgMaster.PageCount <= 1 ? true : false;
            }
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView row = e.Item.DataItem as DataRowView;

                double amount = Convert.ToDouble(row["AMOUNT"]);
                string currencyCode = row["CURRENCYCODE"].ToString();

                Conversion Oc = new Conversion();
                e.Item.Cells[rgMaster.Columns.FindByUniqueName("AMOUNT").OrderIndex].Text = Oc.CNumS2C(amount) + " " + currencyCode;
            }
        }

        protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
        {
            base.RaisePostBackEvent(source, eventArgument);
            switch (eventArgument)
            {
                case "Rebind":
                    rgMaster.Rebind();
                    break;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rgMaster.Rebind();

        }
    }
}